﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;

namespace Pg.BioMedics.SDR.Data.Strategy
{
    /// <summary>
    /// Brovides data mapper managment strategy for default configuration. This 
    /// strategy creates a new DataMapper for each newly created instance 
    /// </summary>
    internal class DefaultDataMapperStrategy : IDataMapperStrategy
    {
        #region Fields
        
        private ISqlMapper _dataMapper;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates instance of default data mepper strategy. This constuctor also initializes 
        /// data mapper basing on default SqlMap.config file.
        /// </summary>
        internal DefaultDataMapperStrategy()
        {
            try
            {
                Stream stream = Assembly.GetExecutingAssembly()
                                        .GetManifestResourceStream("Pg.BioMedics.SDR.Data.SqlMap.default.config");

                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                _dataMapper = builder.Configure(stream);
            }
            catch (Exception ex)
            {
                throw new SDRDataException("Can't initialize data mapper", ex);
            }
        }

        #endregion

        #region IDataMapperStrategy Members

        /// <summary>
        /// Gets instance of initialized data mapper
        /// </summary>
        public ISqlMapper DataMapper
        {
            get 
            { 
                return _dataMapper; 
            }
        }

        #endregion
    }
}
